package com.company.project.search.util;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public final class OkHttpUtil {

    private static final OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .connectTimeout(20, TimeUnit.SECONDS)
            .readTimeout(20, TimeUnit.SECONDS)
            .writeTimeout(20, TimeUnit.SECONDS)
            .build();

    public static OkHttpUtil getInstance() {
        okHttpClient.dispatcher().setMaxRequests(10240);
        okHttpClient.dispatcher().setMaxRequestsPerHost(512);
        return Holder.ins;
    }

    private static final class Holder {
        private static final OkHttpUtil ins = new OkHttpUtil();
    }

    private OkHttpUtil() {

    }

    public Response execute(Request request) throws IOException {
        return okHttpClient.newCall(request).execute();
    }

    public void enqueue(Request request, Callback callback) {
        okHttpClient.newCall(request).enqueue(callback);
    }

}
